package org.gangcai.mac.shop.responsibility;

import com.blankj.utilcode.util.SPUtils;

import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.bean.GetPartMoneyBean;
import org.gangcai.mac.shop.bean.ReBean;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.rxlife.RxActivity;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import io.reactivex.Observable;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by mac on 2018/4/4.
 */

public class DataResponsibility {

    private static DataResponsibility mInstance;

    private DataResponsibility() {
    }

    public static DataResponsibility getInstance() {
        if (mInstance == null) {
            mInstance = new DataResponsibility();
        }
        return mInstance;
    }


    public Observable<CommonResonseBean> submitDataToServer(String url, HashMap<String, String> paramMap, HashMap<String, File> fileMap, RxActivity activity) {

        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        if (paramMap != null) {

            Set<Map.Entry<String, String>> entries = paramMap.entrySet();
            Iterator<Map.Entry<String, String>> iterator = entries.iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> next = iterator.next();
                builder.addFormDataPart(next.getKey(), next.getValue());
            }
        }
        if (fileMap != null) {
            Iterator<Map.Entry<String, File>> iterator1 = fileMap.entrySet().iterator();
            while (iterator1.hasNext()) {
                Map.Entry<String, File> next = iterator1.next();
                builder.addFormDataPart(next.getKey(), next.getValue().getName(), RequestBody.create(MediaType.parse("image/*"), next.getValue()));
            }
        }

        MultipartBody build = builder.build();
        return RetrofitManager.create().authoData(url, build).
                compose(activity.<CommonResonseBean>bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread());

    }

    /**
     * @param stage_id
     * @param type     还款方式1部分还款 2全部还款
     * @param composer
     * @return
     */
    public Observable<GetPartMoneyBean> getPartMoney(String stage_id, String type, ObservableTransformer<? super GetPartMoneyBean, GetPartMoneyBean> composer) {
        String uid = SPUtils.getInstance().getString("uid");
        Observable<GetPartMoneyBean> objectObservable = RetrofitManager.create().get_partmoney(type, uid, stage_id).compose(composer).observeOn(AndroidSchedulers.mainThread());
        return objectObservable;
    }


    /**
     * 支付宝还款
     *
     * @param stage_id
     * @param type     还款方式1部分还款 2全部还款
     * @param composer
     * @param money
     * @return
     */
    public Observable<ReBean> alipay_repayment(String stage_id, String type, ObservableTransformer<? super ReBean, ReBean> composer, String money) {
        String uid = SPUtils.getInstance().getString("uid");
        Observable<ReBean> objectObservable = RetrofitManager.create().alipay_repayment( uid,type, money,stage_id).compose(composer).observeOn(AndroidSchedulers.mainThread());
        return objectObservable;
    }

}
